<template>
  <Modal
    title="添加"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formTybkCooperation"
      :model="formTybkCooperation"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >

      <FormItem label="公司名称" prop="corporate_name">
        <Input type="text" v-model="formTybkCooperation.corporate_name" placeholder="公司名称"/>
      </FormItem>
      <FormItem label="联系人" prop="cooperation_contacts">
        <Input type="text" v-model="formTybkCooperation.cooperation_contacts" placeholder="联系人"/>
      </FormItem>
      <FormItem label="联系电话" prop="cooperation_phone">
        <Input type="text" v-model="formTybkCooperation.cooperation_phone" placeholder="联系电话"/>
      </FormItem>
      <FormItem label="业务范围" prop="cooperation_business_range">
        <Input type="text" v-model="formTybkCooperation.cooperation_business_range" placeholder="业务范围"/>
      </FormItem>
      <FormItem label="合作价" prop="cooperation__cooperative_price">
        <Input type="text" v-model="formTybkCooperation.cooperation__cooperative_price" placeholder="合作价"/>
      </FormItem>
      <FormItem label="市场价" prop="cooperation_market_price">
        <Input type="text" v-model="formTybkCooperation.cooperation_market_price" placeholder="市场价"/>
      </FormItem>

      <FormItem>
        <Button type="primary" @click="handleSubmit('formTybkCooperation')">提交</Button>
        <Button @click="handleReset('formTybkCooperation')" style="margin-left: 8px">重置</Button>
      </FormItem>
    </Form>
  </Modal>
</template>
<script>
import { save } from '@/api/cooperation'
import { createModelObj } from '@/libs/util'

export default {
  name: 'Add',
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formTybkCooperation: {

        id: ''
        ,
        corporate_name: ''
        ,
        cooperation_contacts: ''
        ,
        cooperation_phone: ''
        ,
        cooperation_business_range: ''
        ,
        cooperation__cooperative_price: ''
        ,
        cooperation_market_price: ''
        ,
        deleted: null
        ,
        create_time: ''
        ,
        modification_time: ''
        ,
        deletion_time: ''
      },
      ruleValidate: {

        id: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ]
        ,
        corporate_name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        cooperation_contacts: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 55, message: '长度最长为55', trigger: 'blur' }
        ]
        ,
        cooperation_phone: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ]
        ,
        cooperation_business_range: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        cooperation__cooperative_price: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 10, message: '长度最长为10', trigger: 'blur' }
        ]
        ,
        cooperation_market_price: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 10, message: '长度最长为10', trigger: 'blur' }
        ]
        ,

      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          save(Object.assign({}, createModelObj(this.formTybkCooperation, 'tybkCooperation'))).then(
            res => {
              if (res) {
                this.$emit('addRow', res.row)
                this.$emit('input', false)
                this.handleReset(name)
              }
            }
          )
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  }
}
</script>
